<!-- 验证插件 -->
<script src="/static/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="/static/plugins/jquery-validation/localization/messages_zh.min.js"></script>
<!-- 针对当前主题进行简单封装，精简代码 -->
<script src="/static/sdtheme/scripts/sdvalidate.js"></script>
<!-- 文件上传插件 -->
<script src="/static/plugins/ajaxfileupload.js"></script>
<!-- 图片上传插件 -->
<script src="/static/plugins/bootstrap-fileinput-jasny/bootstrap-fileinput.js"></script>s
<!-- CK Editor -->
<script src="/static/ckeditor/ckeditor.js"></script>
{{/*<script src="/static/plugins/ckeditor/ckeditor.js"></script>*/}}
<script>
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
   $(function () {
       updloadCtrolInit();
       //验证
       validateInfo();
    });
    //表单验证及AJAX提交
    function validateInfo() {
        var $thisForm = $('#thisForm');
        $thisForm.sdvalidate({
            rules: {
                Name: {
                    maxlength: 32,
                    required: true
                },
                // Content: {
                //     required: true
                // }
            },
            messages: {},
            submitHandler: function (form) {
				var url ='{{ urlfor "fronted_manage/RecruitController.Edit"}}';
				//var data = $thisForm.serialize();
                var content = CKEDITOR.instances.editor1.getData();
                var name = $("#Name").val();
                var sort = $("#Sort").val();
                var id = $("#Id").val();
                var imageUrl = $("#ImageUrl").val();
                var data = {Name: name, Sort:sort, Content:content,Id:id,ImageUrl:imageUrl};
                console.log(data);

                    $.sdpost(url,data,function(re){
					if(re.code === 0){
						parent.layer.msg(re.msg);
						parent.refresh(re.obj);
						parent.layer.close(index);
					}else{
						parent.layer.alert(re.msg,{icon:2,title:'失败'})
					}
				});
               return false;
            }
        });
    }
    //image upolad init
    function updloadCtrolInit() {
        //多个图共用一套上传代码
        $(".fileinput").on('change.bs.fileinput', function () {
            var $this = $(this);
            var fileinput = $("input[type='file']", this);
            // file 控件的id
            var inputId = fileinput.attr('id');
            //获取隐藏用于保存图片地址的input
            var hiddeninput = $("#" + inputId.replace('input', ''));
            $.ajaxFileUpload({
                url: '{{ urlfor "UserCenterController.UploadImage" }}',
                secureuri: false,
                fileElementId: inputId,
                dataType: 'json',
                data: { type: 1 }, //用来传file其他的值
                complete: function () {
                    //结束后做一次验证
                    hiddeninput.valid();
                },
                success: function (re, status) {
                    if (re.code === 0) {
                        hiddeninput.val(re.obj);
                    }
                    else {
                        parent.layer.alert(re.msg, { icon: 2, title: '错误' });
                        $this.fileinput('clear');
                        //调用clear方法时不激发事件
                        hiddeninput.val('');
                    }
                },
                error: function (XHR, status, e) {
                    sdtheme.alertXHRError(XHR);
                    $this.fileinput('clear');
                    //调用clear方法时不激发事件
                    hiddeninput.val('');
                }
            });
        });
        //多个图片共用一套上传代码,在上传代码的clear事件里，清空图片地址
        $(".fileinput").on('clear.bs.fileinput', function () {
            var inputId = $("input[type='file']", this).attr('id');
            var hiddeninput = $("#" + inputId.replace('input', ''));
            hiddeninput.val('');
            //这里验证
            //hiddeninput.valid();
        });

    }
    function FormSubmit() {
        $("#thisForm").submit();		
    }
    $(function () {
        // Replace the <textarea id="editor1"> with a CKEditor
        // instance, using default configuration.
        CKEDITOR.replace('editor1', {
            filebrowserUploadUrl: '{{ urlfor "HomeController.CkeditorUpload" }}',
        });
        //bootstrap WYSIHTML5 - text editor
        //$(".textarea").wysihtml5();
    });
</script>